package com.eascs.web.o2o.web.reward.controller;

import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.eascs.app.dao.PageResult;
import com.eascs.common.doc.excel.ExcelApiHelper;
import com.eascs.common.exception.RuntimeIOException;
import com.eascs.common.util.UrlUtil;
import com.eascs.site.view.ViewContext;
import com.eascs.tag.MenuTag;
import com.eascs.web.o2o.web.reward.entity.LoanDetailForm;
import com.eascs.web.o2o.web.reward.entity.Province;
import com.eascs.web.o2o.web.reward.service.CmRdRelationService;
import com.eascs.web.o2o.web.reward.service.ProvinceNameData;
import com.eascs.web.o2o.web.util.BankRowWriteHandler;

@Controller
@RequestMapping(value="/cmRdRelation")
public class RewardCmRdRelationController {
	
	@Autowired
	CmRdRelationService cmRdRelationService;
	
	@Autowired
	ProvinceNameData provinceNameData;

	@RequestMapping(value="/index")
	public String index(LoanDetailForm entity) {
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "cmRdRelation");
		Calendar c = Calendar.getInstance();
		c.add(Calendar.MONTH, -1);
		String date=new SimpleDateFormat("yyyy-MM").format(c.getTime());
		String month = entity.getCheckMonth()==null?date:entity.getCheckMonth();
		entity.setCheckMonth(month);
		String o2oUrl = "/restCmRdRelation/cmRdRelationList";
		if(entity != null) {
			String checkMonth = entity.getCheckMonth();
			String provinceName = entity.getProvinceName();
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "checkMonth", checkMonth);
			o2oUrl = UrlUtil.appendUrl(o2oUrl, "provinceName", provinceName);
			o2oUrl = o2oUrl + "&showPage=";
		} else {
			o2oUrl = o2oUrl + "?showPage=";
		}
		List<Province> proList = provinceNameData.getProvinceNameData();
		ViewContext.put("vo", entity);
		ViewContext.put("o2oUrl", o2oUrl);
		ViewContext.put("proList", proList);
		return "/reward/cm_rd_relation";
	}
	
	@RequestMapping(value="/download")
	public void downloadReport(LoanDetailForm entity, HttpServletResponse httpServletResponse, HttpServletRequest request) {
		entity.setPageSize(Integer.MAX_VALUE);

		PageResult<Object[]> pageResult = cmRdRelationService.getCmRdRelationList(entity, "download");
		String       path    = RewardCmRdRelationController.class.getResource("/excelTemplate").getPath();
		StringBuffer s       = new StringBuffer();
		String       outPath = s.append(path).append("/cm_rd_relation.xls").toString();
		try {
			String outfile = "信贷经理与区域总监关系表.xls";
			String userAgent = request.getHeader("User-Agent");
			byte[] bytes     = userAgent.contains("MSIE") ? outfile.getBytes() : outfile.getBytes("UTF-8");
			outfile = new String(bytes, "ISO-8859-1"); // 各浏览器基本都支持ISO编码
			File f = new File(outPath);
			if (!f.exists()) throw new RuntimeIOException("file not found");

			httpServletResponse.setHeader("Content-Disposition", "attachment; filename=" + outfile);
			httpServletResponse.setContentType("application/octet-stream; charset=utf-8");

			// 开始的表格，行，列
			int sheetIndex  = 0;
			int rowIndex    = 1;
			int columnIndex = 0;
			// 包含的列
			int col = 12;
			// 需要统计的列
			int[]  arr = {};
			
			BankRowWriteHandler bankRowWriteHandler = new BankRowWriteHandler(pageResult.getData(), col, arr, null, sheetIndex, rowIndex, columnIndex);
			ExcelApiHelper.writeHandle(httpServletResponse.getOutputStream(), RewardCmRdRelationController.class.getResourceAsStream("/excelTemplate/cm_rd_relation.xls"), bankRowWriteHandler);

		} catch (IOException e) {
			throw new RuntimeIOException(e);
		} 
	}
	
	public static void main(String[] args) {
		//test2();
		
		/*String str1 = new String("SEU") + new String("Calvin"); 
		String str2 ="rf";
		System.out.println(str1.getClass() );
		System.out.println(str2.getClass() );
		//System.out.println(str1.intern());
		//System.out.println(str1.intern() == str1);     
		System.out.println(str1 == "SEUCalvin");  */
		
		
		/*String s1 = new String("SEU") ;
		System.out.println(s1 == "SEU");  
		s1=s1.intern();
		System.out.println(s1.intern() == s1);   
		System.out.println(s1 == "SEU");  
		
		st
		String str1 = new String("SEU") + new String("Calvin"); 
		//System.out.println(str1.intern() == str1);
		System.out.println(str1.hashCode()+"-"+str1.getClass());
		str1.intern();
		System.out.println(str1.hashCode());
		System.out.println(str1 == "SEUCalvin"); */
		StringBuffer a=new StringBuffer("11"+"123");
		String b="11123";
		System.out.println(a.toString()==b);
		//StringBuffer a=new StringBuffer("11"+"123");
		HashMap<String, String> map=new HashMap<String, String>();
		
	}
	
}
